import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../dd_home/pages/dd_home_page.dart';
import '../../dd_home/pages/dd_home_second_page.dart';
import '../../dd_home/pages/dd_matching_page.dart';
import '../../dd_personal_center/pages/dd_personal_center_page.dart';
import '../../dd_publish/pages/dd_publish_page.dart';
import '../../dd_video/pages/dd_video_page.dart';
import '../controllers/dd_main_tab_controller.dart';
import '../views/dd_index_stack_complete.dart';

class DDMainTabPage extends StatefulWidget {
  const DDMainTabPage({Key? key}) : super(key: key);

  @override
  State<DDMainTabPage> createState() => _DDMainTabPageState();
}

class _DDMainTabPageState extends State<DDMainTabPage> {
  @override
  Widget build(BuildContext context) {
    return GetBuilder<DDMainTabController>(
      init: DDMainTabController(),
      initState: (state) {},
      dispose: (state) {},
      builder: (controller) {
        return Obx(() {
          return controller.showMatching.value
              ? DDMatchingPage()
              : Scaffold(
                  body: DDIndexStack(
                    itemCount: controller.tabList.length,
                    index: controller.tabIndex.value,
                    currentItem: (ctx, index) {
                      switch (index) {
                        case 0:
                          return DDHomePage();
                        case 1:
                          return DDVideoPage();
                        case 2:
                          return DDPublishPage();
                        case 3:
                          return DDPerSonalCenterPage();
                        default:
                          return Scaffold(
                            body: Center(
                              child: Text('other'),
                            ),
                          );
                      }
                    },
                  ),
                  bottomNavigationBar: Container(
                    decoration: BoxDecoration(
                      boxShadow: [
                        BoxShadow(
                          color: Colors.black.withOpacity(0.1),
                          offset: Offset(0, -10),
                          blurRadius: 20,
                          spreadRadius: 1,
                        ),
                      ],
                    ),
                    child: BottomNavigationBar(
                      backgroundColor: Colors.white,
                      currentIndex: controller.tabIndex.value,
                      type: BottomNavigationBarType.fixed,
                      items: controller.tabList.map((tab) {
                        return BottomNavigationBarItem(
                          icon: icon(iconName: tab.iconName),
                          activeIcon: icon(iconName: tab.selectedIconName),
                          label: tab.title,
                          tooltip: '', // 防止长按item出现浮窗
                        );
                      }).toList(),
                      onTap: (index) {
                        controller.tabIndexChange(index);
                      },
                    ),
                  ),
                );
        });
      },
    );
  }

  Widget icon({required String iconName}) {
    return Image.asset(
      iconName,
      gaplessPlayback: true,
    );
  }
}
